package com.ytf.test.third;

/**
 * @author: YTF
 * @date: 2024/9/13 10:57
 * @version: 1.0
 * @description: 位移测试操作
 */
public class ShiftOperation {
    public static void main(String[] args) {
        int i = 1;
            i = -1;
        int iResult;
        System.out.println("i: " + i + " binary:" + Integer.toBinaryString(i));
        iResult = i << 10;
        System.out.println("i<<10: " + iResult + " binary:" + Integer.toBinaryString(iResult));
        iResult = i >> 10;
        System.out.println("i>>10: " + iResult + " binary:" + Integer.toBinaryString(iResult));
        iResult = i >>> 10;
        System.out.println("i>>>10: " + iResult + " binary:" + Integer.toBinaryString(iResult));
        i >>>= 10;
        System.out.println("i>>>=10: " + i + " binary:" + Integer.toBinaryString(i));
        short s = -1;
        System.out.println("s: " + s + " binary:" + Integer.toBinaryString(s));
        s >>>= 10;
        System.out.println("s>>>=10: " + s + " binary:" + Integer.toBinaryString(s));
        byte b =-1;
        System.out.println("b: " + b + " binary:" + Integer.toBinaryString(b));
        b >>>= 10;
        System.out.println("b>>>=10: " + b + " binary:" + Integer.toBinaryString(b));
        long l =-1;
        System.out.println("l: " + l + " binary:" + Long.toBinaryString(l));
        l >>>= 10;
        System.out.println("l>>>=10: " + l + " binary:" + Long.toBinaryString(l));

        // 注意类型在转换时会被截断导致结果和预期不一致
        System.out.println("转换截断过程展示");
        System.out.println("截断前 i: " + i + " binary:" + Integer.toBinaryString(i));
        System.out.println("截断中 short si = (short) i");
        short si = (short) i;
        System.out.println("截断后 si"+si+" binary:" + Integer.toBinaryString(si));
        System.out.println("大于截断位数就没有问题了");
        int csi = -1;
        iResult = csi>>>17;
        System.out.println("int csi =-1 csi>>>=17: " + (iResult) + " binary:" + Integer.toBinaryString(iResult));
        short css=-1;
        iResult = css>>>17;
        System.out.println("short css =-1 css>>>=17: " + (iResult) + " binary:" + Integer.toBinaryString(iResult));

        /**
         * Output:
         * i: -1 binary:11111111111111111111111111111111
         * i<<10: -1024 binary:11111111111111111111110000000000
         * i>>10: -1 binary:11111111111111111111111111111111
         * i>>>10: 4194303 binary:1111111111111111111111
         * i>>>=10: 4194303 binary:1111111111111111111111
         * s: -1 binary:11111111111111111111111111111111
         * s>>>=10: -1 binary:11111111111111111111111111111111
         * b: -1 binary:11111111111111111111111111111111
         * b>>>=10: -1 binary:11111111111111111111111111111111
         * l: -1 binary:1111111111111111111111111111111111111111111111111111111111111111
         * l>>>=10: 18014398509481983 binary:111111111111111111111111111111111111111111111111111111
         * 转换截断过程展示
         * 截断前 i: 4194303 binary:1111111111111111111111
         * 截断中 short si = (short) i
         * 截断后 si-1 binary:11111111111111111111111111111111
         * 大于截断位数就没有问题了
         * int csi =-1 csi>>>=17: 32767 binary:111111111111111
         * short css =-1 css>>>=17: 32767 binary:111111111111111
         * 总结：
         *  << 位移过程中补0 >> 位移过程中根据符号位进行补充  >>> 在位移过程中补0
         *  就算同样的操作符，在不同类型下操作也可能发生其他问题，在使用过程中非常需要注意。
         *  推断不同位数的电脑也可能存在类似跨域截断问题。细心耐心的去解决积累经验
         */
    }
}
